package com.boe.course.web.service.approval.server;

import com.boe.course.common.util.ClassUtil;
import com.boe.course.common.util.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author liuyjy
 * @Date 2024/10/29 16:23
 * @Description: bpm审批对接策略核心
 **/
@Component
@Slf4j
public class ServerEngine {

    private static ServerEngine instance = new ServerEngine();
    @Autowired
    private ApplicationContext applicationContext;
    private Map<String, ApprovalServer> handlerMap = new HashMap<>();

    private Set<Integer> typeSet=new HashSet<>();

    public static ServerEngine getInstance() {
        return instance;
    }

    /**
     * 进行初始化
     * 模块
     */
    @PostConstruct
    public void init() {
        // 备份处理器初始化
        String pack = ApprovalServer.class.getPackage().getName();
        Set<Class<?>> claList = ClassUtil.getClasses(pack);
        try {
            for (Class<?> cla : claList) {
                if (cla.isInterface()) {
                    continue;
                }
                if (cla.isAnnotation()) {
                    continue;
                }
                if (cla.isEnum()) {
                    continue;
                }
                if (ClassUtil.isAbstract(cla)) {
                    continue;
                }
                if (ApprovalServer.class.isAssignableFrom(cla)) {
                    SpringContextUtils util = new SpringContextUtils();
                    util.setApplicationContext(applicationContext);
                    ApprovalServer handler = (ApprovalServer) SpringContextUtils.getBeanByClass(cla);
                    String uniqueKey = handler.getType() + "";

                    if (instance.handlerMap.containsKey(uniqueKey)) {
                        log.error("发现重复的Handler,名称：{}，class:{}", uniqueKey, cla.getName());
                        throw new Exception("重复的PreClickHandler" + cla.getName());
                    } else {
                        log.info("加载模块：{}，class:{}", uniqueKey, cla.getName());
                        instance.handlerMap.put(uniqueKey, handler);
                        instance.typeSet.add(handler.getType());
                    }
                }
            }
        } catch (Exception e) {
            log.error("加载Handler失败", e);
            System.exit(-1);
        }
    }

    public Set<Integer> getTypeList(){
        return instance.typeSet;
    }
    /**
     * 通过对象标记获取指定的对象
     *
     * @param type 对象标记
     * @return
     */
    public ApprovalServer getHandler(int type) {
        String uniqueKey = type + "";
        ApprovalServer handler = instance.handlerMap.get(uniqueKey);
        // 根据广告组ID来确定处理器
        if (handler != null) {
            return handler;
        } else {
            return null;
        }
    }
}
